﻿package Server.AHMJ.MDMJ;

public class MajTypeDef {

};

/*
 * // 麻将类型结构 class BLOCKSTYLE { static int BLOCK_NULL = 0; // 无 static int
 * BLOCK_CHI = 1; // 吃牌结构 static int BLOCK_SUN = 2; // 顺序结构 static int
 * BLOCK_PENG = 3; // 碰子结构,3个相同 static int BLOCK_KE = 4; // 刻子结构,3个相同,非碰 static
 * int BLOCK_GANG = 5; // 杠子结构,4个相同 static int BLOCK_AN_GANG = 6; // 暗杠 };
 */
// 麻将块结构
class BLOCK {
	int style = Constants.BLOCK_NULL; // 麻将块类型
	int first; // 麻将块第一张牌值
	int origValue = -1;// 吃的那张牌的值，或者碰谁的值
	int OutUserGang = -1; // 谁点杠
};

class MJ_BLOCK extends BLOCK {

};

class PTR_MJ_BLOCK extends BLOCK {

};

// 胡牌结构
class HU {
	// HAND_MJ_MAX这个为常量14
	int jiangMj = Constants.INVALID_CARD; // 将牌
	BLOCK block[] = new BLOCK[(Constants.HAND_MJ_MAX - 2) / 3]; // 麻将块
	int sanMj[] = new int[Constants.HAND_MJ_MAX]; // 将牌

	{
		for (int i = 0; i < block.length; i++) {
			block[i] = new BLOCK();
		}
		for (int i = 0; i < Constants.HAND_MJ_MAX; i++) {
			sanMj[i] = Constants.INVALID_CARD;
		}
	}

	void removeLastBlock() {
		for (int i = Constants.MAX_BLOCK_NUM - 1; i >= 0; i--) {
			if (block[i].style != Constants.BLOCK_NULL) {
				block[i].style = Constants.BLOCK_NULL;
				block[i].first = 0;
				break;
			}
		}
	}

	public HU clone() {
		HU hu = new HU();
		hu.jiangMj = jiangMj;
		for (int i = 0; i < block.length; i++) {
			hu.block[i].first = block[i].first;
			hu.block[i].style = block[i].style;
		}
		for (int i = 0; i < Constants.HAND_MJ_MAX; i++) {
			hu.sanMj[i] = sanMj[i];
		}
		return hu;
	}

};

class MJ_HU extends HU {

};

// 杠牌
class GANG {
	int GangMj; // 所杠的牌
	int GangStyle; // 杠牌类型,4-暗杠,2-自摸明杠,1-普通杠,0-无杠
};

class MJ_GANGINFO extends GANG {
};

class BLOCK_GANGINFO {
	int first; // 所杠的牌
	int style; // BLOCK_GANG,BLOCK_ANGANG,BLOCK_BUGANG
	int GangUser;
	int cntLianGang;

	void reset() {
		first = Constants.INVALID_CARD;
		style = 0;
		GangUser = Constants.INVALID_CHAIR;
		cntLianGang = 0;
	}
};

// 吃操作
class ACTION_C_Chi {
	int nChiType;// 吃牌类型,0-无,1-@@, 2-@@, 3-@@
};

// 碰操作
class ACTION_C_Peng {
	int byOutMj;// 碰的牌，可不填
};
// 碰操作无需参数，所以不单独列

// 杠操作
class ACTION_C_Gang {
	int nGangMode;// 杠牌类型,0-无, 1-明杠,2-补杠, 4-暗杠
	int byGangMj;// 所杠的牌，自摸可以有多种杠牌的可能
};

class MajSide {
	int Maj[] = new int[Constants.HAND_MJ_MAX];
	int Hua[] = new int[8];// 代表花
	HU hu = new HU();
	int outMj[] = new int[20];// 每个玩家出去的牌
	int byTing; // 第几张牌听
	int byBaoZi;// 是不是豹子

	boolean IsEnd() {
		return outMj[19] != Constants.INVALID_CARD;
	}

	void Init() {
		CleanHu(hu);
		byTing = 0;
		byBaoZi = 0;
		Constants.memset(Maj, Constants.INVALID_CARD, 14);
		Constants.memset(Hua, Constants.INVALID_CARD, 8);
		Constants.memset(outMj, Constants.INVALID_CARD, 20);
	}

	MajSide clone(MajSide t) {
		Maj = t.Maj.clone();
		Hua = t.Hua.clone();
		hu = t.hu.clone();
		outMj = t.outMj.clone();
		byTing = t.byTing;
		return this;
	}

	// 清除胡牌数据
	void CleanHu(HU hu) {
		hu.jiangMj = (int) Constants.INVALID_CARD;
		for (int i = 0; i < (Constants.HAND_MJ_MAX - 2) / 3; i++) {
			hu.block[i].first = (int) Constants.INVALID_CARD;
			hu.block[i].style = Constants.BLOCK_NULL;
		}
	}

	void SortCard() {
		SortCard((int) (Constants.HAND_MJ_MAX - 1));
	}

	void SortCard(int byHandMjCount) {
//		if (dingque == 0) {
//			int newbyHandCard[] = new int[14];
//			for (int k = 0; k < Maj.length; k++) {
//				if ((dingque == 1 && 10 < Maj[k] && Maj[k] < 37)
//						|| (dingque == 2 && (0 < Maj[k] && Maj[k] < 10) || (20 < Maj[k] && Maj[k] < 37))
//						|| (dingque == 3 && ((0 < Maj[k] && Maj[k] < 20) || (30 < Maj[k] && Maj[k] < 38)))) {
//					newbyHandCard[k] = Maj[k];
//				}
//			}
//			for (int i = 0; i < newbyHandCard.length - 1; i++) {
//				for (int j = i; j < newbyHandCard.length; j++) {
//					if (newbyHandCard[i] > newbyHandCard[j]) {
//						int tempvalue = newbyHandCard[i];
//						newbyHandCard[i] = newbyHandCard[j];
//						newbyHandCard[j] = tempvalue;
//					}
//				}
//			}
//			for (int h = 0; h < Maj.length; h++) {
//				if ((dingque == 1 && 0 < Maj[h] && Maj[h] < 10) || (dingque == 2 && 10 < Maj[h] && Maj[h] < 20)
//						|| (dingque == 3 && 20 < Maj[h] && Maj[h] < 30)) {
//					newbyHandCard[h] = Maj[h];
//				}
//			}
//		} else {
			for (int i = 0; i < byHandMjCount - 1; i++) {
				for (int j = i; j < byHandMjCount; j++) {
					int x = Maj[i];
					int y = Maj[j];
					if (x > y) {
						int tempvalue = Maj[i];
						Maj[i] = Maj[j];
						Maj[j] = tempvalue;
					}
				}
			}
		}
//	}

	int GetHandMjCount() {
		int byHandMjCount = 0;
		for (int byIndex = 0; byIndex < Constants.HAND_MJ_MAX; byIndex++) {
			if (MajHelper.IsWTSZ(Maj[byIndex]) && Maj[byIndex] != Constants.INVALID_CARD) {
				byHandMjCount++;
			}
		}
		return byHandMjCount;
	}

	int GetTotalMjCount() {
		int byHandMjCount = 0;
		for (int byIndex = 0; byIndex < Constants.HAND_MJ_MAX; byIndex++) {
			if (MajHelper.IsWTSZ(Maj[byIndex]) && Maj[byIndex] != Constants.INVALID_CARD) {
				byHandMjCount++;
			}
		}
		for (int byIndex = 0; byIndex < Constants.MAX_BLOCK_NUM; byIndex++) {

			if (hu.block[byIndex].style != Constants.BLOCK_NULL) {
				byHandMjCount = byHandMjCount + 3;
			}

		}
		return byHandMjCount;
	}

	int GetMoPai() {
		return Maj[Constants.HAND_MJ_MAX - 1];
	}

	void SetMoPai(int byMoPai) {
		Maj[Constants.HAND_MJ_MAX - 1] = (int) byMoPai;
	}

	// 桌面加了一张出牌
	void ChuPai(int byChuPai) {
		for (int byIndex = 0; byIndex < 20; byIndex++) {
			if (outMj[byIndex] == Constants.INVALID_CARD) {
				outMj[byIndex] = byChuPai;
				break;
			}
		}
	}

	int GetChuPaiCount() {
		for (int byIndex = 0; byIndex < 20; byIndex++) {
			if (outMj[byIndex] == Constants.INVALID_CARD) {
				return byIndex;
			}
		}

		return 20;
	}

	// 牌被别人吃掉或者碰掉扛掉
	void BeiChiPai() {
		for (int byIndex = 19; byIndex >= 0; byIndex--) {
			if (outMj[byIndex] != Constants.INVALID_CARD) {
				outMj[byIndex] = (int) Constants.INVALID_CARD;
				break;
			}
		}
	}

	int GetHandMajCount(int byMajValue) {
		int byHandMjCount = 0;
		for (int byIndex = 0; byIndex < Constants.HAND_MJ_MAX - 1; byIndex++) {
			if (Maj[byIndex] == byMajValue) {
				byHandMjCount++;
			}
		}
		return byHandMjCount;
	}

	void AddHua(int byHua) {
		int byHuaCount = 0;
		for (int byIndex = 0; byIndex < 8; byIndex++) {
			if (MajHelper.IsHua(Hua[byIndex])) {
				byHuaCount++;
			}
		}
		Hua[byHuaCount] = byHua;
	}

	void JiaoHuan(MajSide t) {
		JiaoHuan(t, false);
	}

	void JiaoHuan(MajSide t, boolean bLastMj) {
		int byMj = Maj[Constants.HAND_MJ_MAX - 1];
		int byMjt = t.Maj[Constants.HAND_MJ_MAX - 1];
		MajSide temp = this;
		// this = t;
		t = temp;
		if (!bLastMj) {
			t.Maj[Constants.HAND_MJ_MAX - 1] = byMjt;
			Maj[Constants.HAND_MJ_MAX - 1] = byMj;
		}
	}

};
